<template>
  <div class="outBill">
    <div class="publicSearch">
      <div class="searchBar" :style="{ height: isMoreSearch ? 'auto' : '' }">
        <el-button-group>
          <el-button @click="getOutData" size="mini" icon="el-icon-search"
            >查询</el-button
          >
          <el-button @click="clearSearchData" size="mini" icon="el-icon-refresh"
            >重置</el-button
          >
        </el-button-group>
        <!-- <div>
            <span>SO类型</span>
            <el-select v-model="searchData.soType" size="mini" filterable placeholder="请选择SO类型" clearable>
                <el-option
                        v-for="item in soBillTypes"
                        :key="item.dataDictDetailId"
                        :label="item.dictValue"
                        :value="item.dataDictDetailId">
                </el-option>
            </el-select>
        </div>
        <div>
            <span>单据来源</span>
            <el-select v-model="searchData.billSource" size="mini" filterable placeholder="请选择单据来源" clearable>
                <el-option :key="1" label="新建" :value="1">新建</el-option>
                <el-option :key="2" label="合并" :value="2">合并</el-option>
                <el-option :key="3" label="调拨" :value="3">调拨</el-option>
            </el-select>
        </div> -->
        <div>
          <span>单号</span>
          <el-input
            v-model="searchData.billNo"
            size="mini"
            placeholder="请输入单号"
            clearable
          ></el-input>
        </div>
        <!-- <div>
            <span>合并单号</span>
            <el-input v-model="searchData.mergeNo" size="mini" placeholder="请输入合并单号" clearable></el-input>
        </div>-->
        
        <!-- <div>
          <span>承运方</span>
          <el-select
            v-model="searchData.carrierId"
            size="mini"
            filterable
            placeholder="请选择承运方"
            clearable
          >
            <el-option
              v-for="(item, index) in carriers"
              :key="index"
              :label="item.carrierName"
              :value="item.carrierId"
            >
            </el-option>
          </el-select>
        </div>
         -->
        <!-- <div>
          <span>货主</span>
          <el-select
            v-model="searchData.organizationId"
            size="mini"
            filterable
            placeholder="请选择货主"
            clearable
          >
            <el-option
              v-for="(item, index) in organizations"
              :key="index"
              :label="item.organizationName"
              :value="item.organizationId"
            >
            </el-option>
          </el-select>
        </div> -->
        <div>
          <span>客户</span>
          <el-select
            v-model="searchData.customerId"
            size="mini"
            filterable
            placeholder="请选择客户"
            style="width: 70%"
          >
            <el-option
              v-for="(item, index) in customers"
              :key="index"
              :label="item.customerName"
              :value="item.customerId"
            >
            </el-option>
          </el-select>
        </div>
        <div>
          <span>创建时间</span>
          <el-date-picker
            v-model="searchData.createTimeArr"
            size="mini"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div>
        <!-- <div>
          <span>要求交货时间</span>
          <el-date-picker
            v-model="searchData.deliveryTimeArr"
            size="mini"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div> -->
        <!-- <div>
          <span>预期发货时间</span>
          <el-date-picker
            v-model="searchData.expectTimeFmArr"
            size="mini"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div> -->
        
        <div>
          <span>关键字搜索</span>
          <el-input
            v-model="searchData.keyWords"
            size="mini"
            title="自定义字段/创建人/备注"
            placeholder="自定义字段/创建人/备注"
            clearable
          ></el-input>
        </div>
      </div>
      <span
        class="moreSearchBtn"
        :class="isMoreSearch ? 'el-icon-caret-top' : 'el-icon-caret-bottom'"
        @click="isMoreSearch = !isMoreSearch"
      ></span>
    </div>
    <div class="headerBtns">
      <!-- <el-button
        size="mini"
        type="primary"
        round
        icon="iconfont icon-xinzeng"
        @click="$router.push({ name: 'createOutBill' })"
      >
        新增
      </el-button> -->
      
      <!-- <el-button
        size="mini"
        type="primary"
        round
        icon="iconfont icon-xinzeng"
        @click="setCarrier()"
      >
        分配承运人
      </el-button> -->

      <el-button
        size="mini"
        type="primary"
        round
        icon="iconfont icon-wuliu"
        :disabled="multipleSelection.length != 1"
        @click="sendOut()"
      >发货</el-button>
      
    </div>
    <div class="tableBox">
      <el-table
        v-loading="loading.masterTable"
        highlight-current-row
        :data="outBills"
        @selection-change="handleSelectionChange"
        style="width: 100%"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <!-- <el-table-column
                 fixed="left"
                 type="index"
                 width="50">
         </el-table-column> -->
         <el-table-column
          show-overflow-tooltip
          prop="outMasterId"
          label="outMasterId"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          fixed="left"
          show-overflow-tooltip
          prop="billNo"
          label="单据号"
          width="180"
        >
          <template slot-scope="scope">
            <span
              class="textBtn"
              @click="$router.push({ name: 'outBillDetail', params: scope.row })"
              >{{ scope.row.billNo }}</span
            >
          </template>
        </el-table-column>
        
        <el-table-column
          show-overflow-tooltip
          prop="soMasterCode"
          label="出库任务号"
          min-width="100"
        >
        </el-table-column>
        
        <el-table-column
          show-overflow-tooltip
          prop="state"
          label="状态"
          min-width="80"
        >
        <template slot-scope="scope">
            <el-tag
              v-if="scope.row.state == 0"
              type="info"
              >新建
            </el-tag>
            <el-tag
              v-if="scope.row.state == 8"
              type="success"
              >已出库
            </el-tag>
            <el-tag
              v-if="scope.row.state == 10"
              type="danger"
              >已签收
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
                show-overflow-tooltip
                prop="soType"
                label="任务类型"
                min-width="100">
            <template slot-scope="scope">
                <span v-for="(item,index) in soBillTypes" :key="index" v-if="scope.row.soType == item.dictKey">{{item.dictValue}}</span>
            </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="organizationName"
          label="货主"
          min-width="100"
        >
          <template slot-scope="scope">
            <span
              class="textBtn"
              @click="
                $EventBus.$emit(
                  'showPublicModal',
                  scope.row.organizationId,
                  'organization'
                )
              "
              >{{ scope.row.organizationName }}</span
            >
          </template>
        </el-table-column>
        <!-- <el-table-column
                show-overflow-tooltip
                prop="customerName"
                label="客户"
                min-width="100">
            <template slot-scope="scope">
                <span class="textBtn" @click="$parent.showPublicModal(scope.row.customerId, 'customer')">{{scope.row.customerName}}</span>
            </template>
        </el-table-column>
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="memo"
          label="备注"
          min-width="180"
        >
        </el-table-column>
        
        <!-- <el-table-column
          show-overflow-tooltip
          prop="shipName"
          label="承运方"
          min-width="120"
        >
        </el-table-column> -->
        <el-table-column
          show-overflow-tooltip
          prop="createTime"
          label="创建时间"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ scope.row.createTime.replace("T", " ") }}
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="createUserName"
          label="创建人"
          min-width="100"
        >
          <template slot-scope="scope">
            <el-button
              @click="
                $EventBus.$emit(
                  'showPublicModal',
                  scope.row.createUserId,
                  'account'
                )
              "
              type="text"
            >
              {{ scope.row.createUserName }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="signTime"
          label="签收时间"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ scope.row.signTime}}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" min-width="80">
          <template slot-scope="scope">
            <el-button
              @click="showNewModal(scope.row)"
              type="primary"
              size="small"
              :disabled="scope.row.state !== 10"
              >签收查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination-bottom"
        style="text-align: right"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        :current-page.sync="searchData.pageNum"
        :page-size="searchData.pageSize"
        :page-sizes="[15, 30, 50, 100]"
        layout="sizes,total, prev, pager, next, jumper"
        :total="searchData.total"
      >
      </el-pagination>
    </div>

    <!--<el-dialog title="签收图片" :visible.sync="previewDialogVisible" width="40%"  class="dialog-picture" >
      <img :src="require(`D:/wms/${imgName}.png`)" width="300" height="300" />
    </el-dialog>-->
  </div>
</template>
<script>
import { mapState } from "vuex";

export default {
  name: "OutBill",
  data() {
    return {
      loading: {
        masterTable: true,
      },

      previewDialogVisible: false,
      imgName: "blob",

      isMoreSearch: false,
      searchData: {
        pageNum: 1,
        pageSize: 15,
        total: null,
        keyWords: "",

        wareId: PF.getLocal("wareId", "number"),
        state: null,
        organizationId: null,
        customerId: null,
        billSource: null,
        carrierId: null,
        soType: null,
        createTimeArr: [],
        startCreateTime: "",
        endCreateTime: "",
        deliveryTimeArr: [],
        startDeliveryTime: "",
        endDeliveryTime: "",
        expectTimeFmArr: [],
        startExpectTimeFm: "",
        endExpectTimeTo: "",
        billNo: "",
        mergeNo: "",
        orderNo: "",
      },
      outBills: [],
      //   formLabelWidth: "120px",
      //   soUdfHs: [],
      organizations: [],
      customers: [],
      soBillTypes: [],
      carriers: [],
      multipleSelection: [],
    };
  },
  computed: {
    ...mapState(["reviewStates"]),
  },
  methods: {
    handleSizeChange(val) {
      this.searchData.pageSize = val;
      this.getOutData();
    },

    handleCurrentChange(val) {
      this.searchData.pageNum = val;
      this.getOutData();
    },
    clearSearchData() {
      this.searchData.keyWords = "";
      this.searchData.state = null;
      this.searchData.organizationId = null;
      this.searchData.customerId = null;
      this.searchData.soType = null;
      this.searchData.billSource = null;
      this.searchData.carrierId = null;
      this.searchData.createTimeArr = [];
      this.searchData.startCreateTime = "";
      this.searchData.endCreateTime = "";
      this.searchData.deliveryTimeArr = [];
      this.searchData.startDeliveryTime = "";
      this.searchData.endDeliveryTime = "";
      this.searchData.expectTimeFmArr = [];
      this.searchData.startExpectTimeFm = "";
      this.searchData.endExpectTimeTo = "";
      this.searchData.billNo = "";
      this.searchData.mergeNo = "";
      this.searchData.orderNo = "";
    },

    showNewModal(row) {
      if (row == null) {
        this.$message({
          message: "请选择一条数据",
          type: "warning"
        });
      } else {
        this.previewDialogVisible = true;
        // 收货编号
        this.imgName = row.billNo;
      }
    },
    getOutData() {
      let searchData = PF.JSON(this.searchData);
      if (searchData.createTimeArr.length > 0) {
        searchData.startCreateTime = searchData.createTimeArr[0];
        searchData.endCreateTime = searchData.createTimeArr[1];
      }
      if (searchData.deliveryTimeArr.length > 0) {
        searchData.startDeliveryTime = searchData.deliveryTimeArr[0];
        searchData.endDeliveryTime = searchData.deliveryTimeArr[1];
      }
      if (searchData.expectTimeFmArr.length > 0) {
        searchData.startExpectTimeFm = searchData.expectTimeFmArr[0];
        searchData.endExpectTimeTo = searchData.expectTimeFmArr[1];
      }
      let that = this;
      that.loading.masterTable = true;
      IOT.getServerData("/out/master/list", "get", searchData, (ret) => {
        that.loading.masterTable = false;
        if (ret.code === 200) {
          this.outBills = ret.rows;
          console.log(JSON.stringify(this.outBills));
          this.searchData.total = ret.total;
          this.searchData.pageNum = ret.pageNum;
          IOT.hideOverlay();
        } else {
          IOT.hideOverlay();
          IOT.tips(ret.message || "服务器请求失败，稍后再试！", "error");
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    getCode() {
      var that = this;
      document.onkeypress = function (e) {
        that.getOutData();
      };
    },

    //签收确认
    sendOut() {
      IOT.showOverlay("保存中，请稍等...");
      let list = PF.JSON(this.multipleSelection);
      var billNos = []
      for (let i = 0; i < list.length; i++) {
          billNos.push(list[i].billNo);
      }

     
      IOT.getServerData(
        "/out/master/sendOut",
        "post",
        { billNos: billNos },
        ret => {
          if (ret.code === 200) {
            IOT.hideOverlay();
            IOT.tips("保存成功！", "success");
            this.getOutData();
          } else {
            IOT.hideOverlay();
            IOT.tips(ret.message || "服务器请求失败，稍后再试！", "error");
          }
        }
      );
    },
  },
  created() {
    this.getOutData();
    // PF.getCarriers((rows) => {
    //   this.carriers = rows;
    // });
    PF.getDataDictUdfData("soBillType", (rows) => {
      this.soBillTypes = rows;
    });
    this.getCode();
  },
  mounted() {},
};
</script>

<style scoped lang="less">
.outBill {
}

.dialog-picture {
  text-align: center;
}
.previewImg {
  width: 56%;
}
</style>
